package com.zzsy.shoppingpro.utils;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

/**
 * Created by Administrator on 2018/7/2 0002.
 * Create name : wangkuo
 * Explain : 处理和屏幕尺寸相关的类
 */

public class ScreenUtils {

    /**
     * 获得屏幕像素的宽高
     */
    public static int getScreenWidth(Context context) {
        if (context != null) {
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            if (windowManager == null) {
                return 0;
            }
            DisplayMetrics outMetrics = new DisplayMetrics();
            windowManager.getDefaultDisplay().getMetrics(outMetrics);
            return outMetrics.widthPixels;
        } else {
            return 0;
        }
    }

    /**
     * 获取屏幕像素的宽度
     */
    public static int getScreenHeight(Context context) {
        if (context != null) {
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            if (windowManager == null) {
                return 0;
            }
            DisplayMetrics displayMetrics = new DisplayMetrics();
            windowManager.getDefaultDisplay().getMetrics(displayMetrics);
            return displayMetrics.heightPixels;

        } else {
            return 0;
        }
    }

    /**
     * 获取屏幕dip
     */
    public static float getDensity(Context context) {
        DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics();
        return dm.density;
    }


    /**
     * 获得状态栏高度，通过Resource类方法获取status_bar_height的具体数值
     */
    public static int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }


    // ============================================ 单位转换 ============================================ //

    /**
     * 根据手机的分辨率 从dp 转 px 保证尺寸大小不变
     */
    public static int dp2px(Context context, float dpValue) {
        if (context != null) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (dpValue * scale + 0.5f);
        } else {
            return 0;

        }
    }

    /**
     * 根据手机的分辨率 从dp 转 px 保证文字大小不变
     */
    public static int sp2px(Context context, float spValue) {
        final float scale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px 转 dp，保证尺寸大小不变
     */
    public static float px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (pxValue / scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px 转 sp，保证文字大小不变
     */
    public static float px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (pxValue / fontScale + 0.5f);
    }

}
